<template>
  <a-card :bordered="false">
    <div style="margin-bottom: 10px;">
      <a-select default-value="1" style="width: 120px" @change="selChange">
        <a-select-option :value="opt.id" v-for="(opt,idx) in typeList" :key="idx">
          {{opt.name}}
        </a-select-option>
      </a-select>
      <a-input-search
        style="width: 520px; margin-left: 10px;"
        placeholder="请输入"
        enter-button="搜索"
        @search="onSearch"
      />
    </div>

    <!-- table区域-begin -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
          <a @click="viewDoc(record)">预览</a>
        </span>

      </a-table>
    </div>

    <a-modal :width="1200" :dialog-style="{ top: '0px' }" :visible="previewVisible" :footer="null" @cancel="handleCancel">
      <viewDoc :viewDocData="viewDocData"></viewDoc>
    </a-modal>

  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import { loadCategoryData } from '@/api/api'
  import { fileDetailsInfoList } from '@/api/digitization'
  import viewDoc from './ViewDoc'
  let Base64 = require('js-base64').Base64;

  export default {
    name: 'SearchView2',
    mixins:[JeecgListMixin],
    components: {
      viewDoc
    },
    data () {
      return {
        typeList:[
          { id:'1', name:'文件名' },
          { id:'2', name:'设备' },
          { id:'3', name:'文件类型' },
        ],
        currType:'1',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key: 'rowIndex',
            align: "center",
            customRender: function (t, r, index) {
              return parseInt(index) + 1;
            }
          },
          {
            title: '文件名',
            dataIndex: 'fileTitle',
            sorter: (a, b) => a.fileTitle.length - b.fileTitle.length,
            align: "center",
          },
          {
            title: '创建人',
            dataIndex: 'createBy',
            align: "center",
          },
          {
            title: '创建日期',
            dataIndex: 'createTime',//排序方法
            sorter: (a, b) => {
              let aTime = new Date(a.createTime).getTime();
              let bTime = new Date(b.createTime).getTime();
              return aTime - bTime;
            },
            align: "center",
          },
          {
            title: '所属设备',
            dataIndex: 'eqpBelong',
            sorter: (a, b) => a.eqpBelong.length - b.eqpBelong.length,
            align: "center",
          },
          {
            title: '文件类型',
            dataIndex: 'fileTypes',
            sorter: (a, b) => a.fileTypes.length - b.fileTypes.length,
            align: "center",
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: "center",
            scopedSlots: {customRender: 'action'},
          }
        ],
        previewVisible: false,
        viewDocData:{},

        url: {
          list: "/base/fmFileDetailsInfo/list",
        },
      }
    },
    created() {
    },
    methods: {
      selChange(v){
        this.currType = v;
      },
      onSearch(v){
        let params = {};
        if(v){
          let _type = '';
          switch (this.currType) {
            case '1':
              _type = 'fileTitle';
              break;
            case '2':
              _type = 'eqpBelong';
              break;
            case '3':
              _type = 'fileTypes';
              break;
          }
          params[_type] = v;
        }
        fileDetailsInfoList(params).then(res => {
          this.dataSource = res.result.records;
          this.ipagination.total = res.result.total;
        })
      },
      viewDoc(record){
        let url = window._CONFIG['staticDomainURL']  + record.filePath; //要预览文件的访问地址
        //let codeUrl = Base64.encode(url);
        let fileSrc = window._CONFIG['fileDomainURL'] + '/onlinePreview?url=' + encodeURIComponent(Base64.encode(url));

        // let routeData = this.$router.resolve({
        //   path: '/digital/viewDoc',
        //   query: {fileSrc, fileData: JSON.stringify(record)},
        // });
        // window.open(routeData.href, '_blank');
debugger
        this.viewDocData = {fileSrc, fileData: JSON.stringify(record)}
        this.previewVisible = true;
      },
      handleCancel() {
        this.previewVisible = false;
        this.viewDocData = {};
      },

    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>